# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import codecs,json
class MypjtPipeline(object):
    def __init__(self):
        print("******open file******")
        self.file = codecs.open("data1.txt","wb",encoding="utf-8")
        
    def process_item(self, item, spider):
        print("******before write to file****")
        #i = str(item) + "\n"   
        print(type(item))
        i = json.dumps(dict(item), ensure_ascii=False)#将数据转化为json格式，并将中文解码
        data = i + "\n"
        print(data)
        print("*******")
        self.file.write(data)
        #self.file.write(type(data))
        print(type(data))
        print("******end write to file****")
        return item
    def close_spider(self,spider):
        self.file.close()
        